package Smallprogram.dao.impl;

import Smallprogram.dao.OrderbiaoDao;
import Smallprogram.entiy.Orderbiao;
import order.dao.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderbiaoDaoImpl extends BaseDao implements OrderbiaoDao {
    @Override
    public List<Orderbiao> selectorderbiao() {
        String sql="SELECT id,timeorder, ordernumber,Userid, Remarks,CASE WHEN Helpyourself = 1 THEN '自取' WHEN Helpyourself = 0 THEN '外卖' ELSE '未知' END AS helpyourself_status, CASE WHEN payment = 1 THEN '余额付款' WHEN payment = 0 THEN '微信' ELSE '未知' END AS payment_status,CASE WHEN Pickup = 1 THEN  '已取餐'WHEN  Pickup = 0 THEN  '未取餐' ELSE '未知' END AS  pickup_status,Pickuptime FROM Orderbiao where Userid=1";
        ResultSet res=this.executeQuery(sql);
        List<Orderbiao> list=new ArrayList<>();
        try {
            while (res.next()){
                list.add(new Orderbiao(res.getInt("id"),res.getString("payment_status"),res.getString("pickup_status"),res.getString("timeorder"), res.getString("Pickuptime"),res.getString("Remarks"),res.getInt("id"),res.getString("ordernumber")));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public List<Orderbiao.Goods> selecgoods() {
        String sql="select * from Detailsdb";
        ResultSet res=this.executeQuery(sql);
        List<Orderbiao.Goods> list =new ArrayList<>();
        try {
            while (res.next()){
                list.add(new Orderbiao.Goods(res.getInt(1), res.getInt(2), res.getInt(3), res.getDouble(4), res.getDouble(5), res.getDouble(6),res.getString(7),res.getInt(8), res.getString(9)));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public int selectMAXOrder() {

        String sql="SELECT MAX(id) FROM `Orderbiao`";
        ResultSet res=this.executeQuery(sql);
      int ine=0;
        try {
            while (res.next()){
                ine=res.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return ine;
    }

    @Override
    public int insertDetailsdb(Orderbiao.Goods goods) {
        String sql="INSERT INTO `BurgerKingDB`.`Detailsdb`(`snacksid`, `Orderbiaoid`, `originAmount`, `price`, `amount`, `name`, `number`) VALUES (?,?,?,?,?,?,?)";
        return this.executeUpdate(sql,goods.getSnacksid(),goods.getOrderbiaoid(),goods.getOriginAmount(),goods.getPrice(),goods.getAmount(),goods.getName(),goods.getNumber());
    }

    @Override
    public int insertOrderbiao(Orderbiao Oeder) {
        String sql="INSERT INTO `BurgerKingDB`.`Orderbiao`(`timeorder`, `ordernumber`, `Userid`, `Remarks`, `Helpyourself`, `payment`, `Pickup`) VALUES (NOW(),?,?,?, ?, 1,0)";
        return  this.executeUpdate(sql,Oeder.getOrder_no(),Oeder.getUserid(),Oeder.getPostscript(),Oeder.getHelpuourslf());
    }

    @Override
    public List<Orderbiao> selectorderbiaoid(int id) {
        String sql="SELECT id,timeorder, ordernumber,Userid, Remarks,CASE WHEN Helpyourself = 1 THEN '自取' WHEN Helpyourself = 0 THEN '外卖' ELSE '未知' END AS helpyourself_status, CASE WHEN payment = 1 THEN '余额付款' WHEN payment = 0 THEN '微信' ELSE '未知' END AS payment_status,CASE WHEN Pickup = 1 THEN  '已取餐'WHEN  Pickup = 0 THEN  '未取餐' ELSE '未知' END AS  pickup_status,Pickuptime FROM Orderbiao where Userid=1  and Pickup=0 and id=?";
        ResultSet res=this.executeQuery(sql,id);
        List<Orderbiao> list=new ArrayList<>();
        try {
            while (res.next()){
                list.add(new Orderbiao(res.getInt("id"),res.getString("payment_status"),res.getString("pickup_status"),res.getString("timeorder"), res.getString("Pickuptime"),res.getString("Remarks"),res.getInt("id"),res.getString("ordernumber")));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }
}
